package com.cjy.service.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.cjy.dao.UserRepository;
import com.cjy.pojo.User;
import com.cjy.service.UserService;
import com.cjy.util.MD5Utils;
import com.cjy.util.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    @Transactional
    public User checkUser(String username, String password) {
        User user = userRepository.findByAccountAndPassword(username,password);
        return user;
    }

    @Override
    public String showUserDescription() {
        String userDescription = userRepository.findById(1L).get().getUserDescription();
        return userDescription;
    }

    @Override
    public User findUserById(Long id) {
        return userRepository.getOne(id);
    }

    @Override
    @Transactional
    public User updateUser(Long id, User user) {
        User one = userRepository.getOne(id);
        user.setPassword(one.getPassword());
        return userRepository.save(user);
    }

    @Override
    public String checkAcount(String account) {
        User user = userRepository.findByAccount(account);
        return user == null ? "ok" : "账号已存在，请重新填写账号";
    }

    @Override
    public String checkEmail(String email) {
        User user = userRepository.findByEmail(email);
        return user == null ? "ok" : "邮箱已存在，请重新填写邮箱";
    }

    @Override
    public String sendCheckNumber(String email) {
        String checkNumber = RandomUtil.randomString(6);
        boolean set = redisUtil.set(checkNumberKey(email), checkNumber, 60);
        if (!set) {
            return "验证码发送失败";
        }
        // 发送邮箱
        MailAccount account = new MailAccount();
        account.setHost("smtp.qq.com");
        account.setAuth(true);
        account.setSslEnable(true);
        account.setFrom("1718707267@qq.com");
        account.setUser("1718707267");
        account.setPass("ghhypltqwudccbig");

        try {
            MailUtil.send(account, CollUtil.newArrayList(email),
                    "广软社区",
                    "注意：为了保护您的账户安全，请勿转发该邮件。您的验证码为"+ checkNumber + ",该验证码有效时间一分钟。",
                    false);
        }catch (Exception e){
            e.printStackTrace();
            return "发送验证码失败";
        }
        return "ok";
    }

    @Override
    public String register(User user, String checkNumber) {
        // 校验验证码
        if (!redisUtil.hasKey(checkNumberKey(user.getEmail()))){
            return "验证码校验失败";
        }
        String redisNumber = (String) redisUtil.get(checkNumberKey(user.getEmail()));
        if (StrUtil.isBlank(redisNumber) || !redisNumber.equals(checkNumber)) {
            return "验证码校验失败";
        }
        if (!ObjectUtil.isNull(userRepository.findByAccount(user.getAccount()))) {
            return "注册失败，账号已经存在";
        }
        if (!ObjectUtil.isNull(userRepository.findByEmail(user.getEmail()))) {
            return "注册失败，邮箱已经存在";
        }
        // 注册实现
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setPassword(MD5Utils.code(user.getPassword()));
        user.setNickname(user.getAccount());
        user.setUsername(user.getAccount());
        user.setAvatar("mIg3i2WeWgmAf-SwAABmnmAwTKY652.jpg");
        user.setType(1);
        user.setUserDescription("");
        user.setRoleId(2);
        User save = userRepository.save(user);
        return save == null ? "注册失败" : "ok";
    }

    @Override
    public Page<User> listUser(Pageable pageable) {
        return null;
    }

    @Override
    public User userSave(User user) {
        return null;
    }

    @Override
    public User getUser(Long id) {
        return null;
    }

    @Override
    public void deleteUser(Long id) {

    }

    public String checkNumberKey(String email) {
        return email + "_CheckNumber";
    }
}
